/**
 * @author jjophin
 */
var OrgChart = function(options){
    this.chartContainer = this.getElement(options.chartContainer);
    this.tableContainer = options.tableContainer && this.getElement(options.tableContainer);
    this.showTable = options.showTable || false;
    this.setTableConf(options.tableConf || undefined);
    this.delimiter = options.delimiter || ',';
    this.chartSize = options.chartSize || "medium";
    this.allowCollapse = options.allowCollapse || false;
};

OrgChart.prototype = (function(){
    var getElement = function(id){
        return id.constructor == String ? document.getElementById(id) : id;
    };
    
    var setTableConf = function(conf){
        this.tableConf = {
            child: {
                colIndex: 0,
                title: "Child Column",
                showInTable: true
            },
            parent: {
                colIndex: 1,
                title: "Parent Column",
                showInTable: true
            },
            info: {
                colIndex: 2,
                title: "Info Column",
                showInTable: true,
                showInChart: true
            },
            tooltip: {
                colIndex: 3,
                title: "Tooltip Column",
                showInTable: false,
                showInChart: true
            }
        };
        if (conf) 
            for (var i in conf) 
                for (var j in conf[i]) 
                    if (j.search("colIndex") == -1) 
                        this.tableConf[i][j] = conf[i][j];
        this.tableConf.parent.showInChart = true;
        this.tableConf.child.showInChart = true;
    };
    
    var setData = function(dataObj){
        var tableConf = this.tableConf;
        var data = [dataObj.child.split(this.delimiter), dataObj.parent.split(this.delimiter), (dataObj.info && dataObj.info.split(this.delimiter)) || [], (dataObj.tooltip && dataObj.tooltip.split(this.delimiter)) || []];
        this.numOfNodes = data[0].length;
        var dataObject = new google.visualization.DataTable();
        for (var i in tableConf) 
            dataObject.addColumn('string', tableConf[i].title);
        dataObject.addColumn("string", "formatted_child");
        for (var i = 0; i < this.numOfNodes; i++) 
            dataObject.addRow([data[0][i], data[1][i], data[2][i] || "", data[3][i] || "", {
                v: data[0][i],
                f: data[0][i] + "<br /><font color='red'>" + (data[2][i] || "") + "</font>"
            }]);
        this.setDataViews(dataObject);
    }
    
    var setDataViews = function(dataObject){
        this.tableDataObject = new google.visualization.DataView(dataObject);
        this.tableDataObject.setColumns([0, 1, 2, 3]);
        this.chartDataObject = new google.visualization.DataView(dataObject);
        this.chartDataObject.setColumns([4, 1, 3]);
    }
    
    var draw = function(){
        var tableConf = this.tableConf;
        for (var i in tableConf) {
            !(tableConf[i].showInTable) && this.tableDataObject.hideColumns([tableConf[i].colIndex]);
            !(tableConf[i].showInChart) && this.chartDataObject.hideColumns([tableConf[i].colIndex]);
        }
        this.chart = new google.visualization.OrgChart(this.chartContainer);
        this.chart.draw(this.chartDataObject, {
            allowHtml: true,
            allowCollapse: this.allowCollapse,
            size: this.chartSize
        });
        this.allowCollapse &&
        google.visualization.events.addListener(this.chart, 'collapse', (function(that){
            return function(){
				_browserCheck(that);
			};
        })(this));
        _browserCheck(this);
        if (this.showTable) {
            this.table = new google.visualization.Table(this.tableContainer);
            this.table.draw(this.tableDataObject);
        }
    }
    
    var chartDisplayControl = function(that, collapsed){
        var numRows = that.chartDataObject.getNumberOfRows();
        for (var i = 0; i < numRows; i++) 
            that.chart.collapse(i, collapsed);
    }
    
    var collapseAll = function(){
        chartDisplayControl(this, true);
        _browserCheck(this);
    }
    
    var expandAll = function(){
        chartDisplayControl(this, false);
        _browserCheck(this);
    }
    
    function _browserCheck(that){
        var isChrome = navigator.userAgent.search("Chrome");
        if (isChrome == -1) 
            return;
        var tableRows = that.chartContainer.childNodes[0].childNodes[0].childNodes;
        var noOfRows = tableRows.length;
        for (var i = 0; i < noOfRows; i++) {
            var noOfColsInRow = tableRows[i].childNodes.length;
            var colsInRow = tableRows[i].childNodes;
            var nodeClass = 'google-visualization-orgchart-node google-visualization-orgchart-node-' + that.chartSize;
            for (var j = 0; j < noOfColsInRow; j++) {
                var className = colsInRow[j].getAttribute('class');
                if (className == nodeClass) {
                    colsInRow[j].setAttribute('style', 'border: 0px;');
                }
            }
        }
    }
    
    return {
        getElement: getElement,
        setTableConf: setTableConf,
        setData: setData,
        setDataViews: setDataViews,
        draw: draw,
        collapseAll: collapseAll,
        expandAll: expandAll
    }
})();